﻿using System;
using System.Collections.Specialized;
using Microsoft.SharePoint;
using Navigant.Infrastructure.Helpers;
using Navigant.Infrastructure.Repositories;

namespace Navigant.Web.UI
{
    public class NavigantCaseConfigurationSettingsFeatureReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPWeb parentWeb = properties.Feature.Parent as SPWeb;
                SPList settingsList = null;
                if (parentWeb != null)
                {
                    if (SharePointCaseRepository.IsValidCaseSite(parentWeb))
                    {
                        foreach (SPList list in parentWeb.Lists)
                        {
                            if (list.Title == SharePointCaseRepository.Constants.SiteSettingsListName)
                            {
                                settingsList = list;
                                break;
                            }
                        }
                        if (settingsList != null)
                        {
                            if (!SharePointCaseRepository.ListContainsStringValueField(settingsList))
                            {
                                // Add the new field
                                settingsList.Fields.Add(SharePointCaseRepository.Constants.SiteSettingStringValueField,
                                    SPFieldType.Note, false);
                            }
                            string viewName = "Configuration Settings";
                            bool containsView = false;
                            foreach (SPView view in settingsList.Views)
                            {
                                if (view.Title == viewName)
                                {
                                    containsView = true;
                                    break;
                                }
                            }
                            if (!containsView)
                            {
                                StringCollection viewFields = new StringCollection();
                                viewFields.Add(SharePointCaseRepository.Constants.SiteSettingNameViewField);
                                viewFields.Add(SharePointCaseRepository.Constants.SiteSettingStringValueField);
                                SPView view = settingsList.Views.Add("Configuration Settings", viewFields, string.Empty,
                                    2000, true, true);
                                view.Query = "<Where><IsNull><FieldRef Name=\"Number_x0020_Value\"/></IsNull></Where>";
                                view.Update();
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logger.LogError(e);
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }
    }
}
